package com.ruoyi.cpsp.service.impl;
/*
    @author neu.xiangwenjuan
    @create 2022/4/15
*/

import com.ruoyi.cpsp.domain.*;
import com.ruoyi.cpsp.mapper.CurrentPerformanceDataMapper;
import com.ruoyi.cpsp.mapper.EvaluateRecordDetailMapper;
import com.ruoyi.cpsp.mapper.EvaluateRecordMapper;
import com.ruoyi.cpsp.mapper.RegulationTypeMapper;
import com.ruoyi.cpsp.service.EvaluateRecordDetailService;
import com.ruoyi.cpsp.service.EvaluateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EvaluateRecordDetailServiceImpl implements EvaluateRecordDetailService {

    @Autowired
    private CurrentPerformanceDataMapper currentPerformanceDataMapper;

    @Autowired
    private EvaluateRecordDetailMapper mapper;

    @Autowired
    private RegulationTypeMapper regulationTypeMapper;

    @Autowired
    private EvaluateRecordMapper evaluateRecordMapper;

    @Autowired
    private EvaluateRecordServiceImpl evaluateRecordServiceImpl;

    @Override
    public List<EvaluateRecordDetailEn> selectAllDetail(String departmentId) {
        return mapper.selectAllDetail(departmentId);
    }

    @Override
    public List<EvaluateRecordDetailEn> selectDetailByEmployee(String employeeIdCard) {
        return mapper.selectDetailByEmployee(employeeIdCard);
    }

    @Override
    public List<EvaluateRecordDetailEn> selectDetailByEmployeeYear(String employeeIdCard, Integer year, Integer quarter) {
        return mapper.selectDetailByEmployeeYear(employeeIdCard, year, quarter);
    }

    @Override
    public List<EvaluateRecordDetailEn> selectByDepart(String department, String teamGroup, String startTime, String endTime) {
        return mapper.selectByDepart(department, teamGroup, startTime, endTime);
    }

    @Override
    public EvaluateRecordDetail selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertDetail(EvaluateRecordDetail evaluateRecordDetail) {
        return mapper.insertDetail(evaluateRecordDetail);
    }

    @Override
    public int deleteRecordDetail(Integer id) {
        return mapper.deleteRecordDetail(id);
    }

    @Override
    public EvaluateRecordDetailEn selectDetailById(Integer id) {
        return mapper.selectDetailById(id);
    }

    @Override
    public int updateByIdSelective(EvaluateRecordDetail evaluateRecordDetail) {
        return mapper.updateByIdSelective(evaluateRecordDetail);
    }

    @Override
    public boolean updateCurrentPerformanceData(List<String> employeeIdCards, EvaluateRecordEntity record) {
        //TODO 年份和季度
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int quarter = evaluateRecordServiceImpl.getSeason(new Date());
        year =  quarter == 1? year - 1 : year;
        quarter = quarter == 1? 4 : quarter - 1;


        // 首先根据身份证号列表得到原始的绩效信息。
        List<CurrentPerformanceDataEn> dataEnList = currentPerformanceDataMapper.selectDataByIdCard(employeeIdCards);
        // 通过查询performance_to_employee表查询这些员工最新的扣减比例。
        List<EvaluateRecordCurrentQuarter> maxProp = evaluateRecordMapper.selectMaxProp(year, quarter, employeeIdCards);

        CurrentPerformanceData currentPerformanceData = new CurrentPerformanceData();
        for (int i = 0; i < dataEnList.size(); i ++) {
            // 判断该员工是违章者，或者是关联人员
            String idCard = dataEnList.get(i).getEmployee().getIdCard();
            boolean isMain = mapper.selectDetailByEmployee(idCard).size() > 0;
            // 判断该员工担任生产岗还是辅助岗
            boolean isDirect = dataEnList.get(i).getEmployee().getIsDirectProducer() == 1;
            boolean isMaxProp = false;
            // 在这里判断违章是否是事故相关，获取一个flag
            boolean isAccident = false;
            if (record != null) {
                // 判断该违章类型是否为事故
                RegulationType regulationType = regulationTypeMapper.selectByPrimaryKey(record.getRegulationTypeId());
                if (regulationType.getCheckSide().equals("事故")) {
                    isAccident = true;
                }
            }
            for (int j = 0; j < maxProp.size(); j ++) {
                if (dataEnList.get(i).getEmployee().getIdCard().equals(maxProp.get(j).getEmployeeIdCard())){
                    // 没有把之前的违章者和被关联者的信息删除，所以有问题。

                    isMaxProp = true;
                    currentPerformanceData.setEmployeeIdCard(maxProp.get(j).getEmployeeIdCard());
                    currentPerformanceData.setEvaluateRecordId(maxProp.get(j).getEmployeeIdCard());
                    if (isMain) {
                        currentPerformanceData.setAnnotation("违章者");
                    }else {
                        currentPerformanceData.setAnnotation("被关联者");
                    }
                    if (isAccident) {
                        currentPerformanceData.setIncreaseBase(0.0);
                    }else {
                        if (isMain) {
                            currentPerformanceData.setIncreaseBase(0.0);
                        }else {
                            if (isDirect) {
                                currentPerformanceData.setIncreaseBase(100.0);
                            }else {
                                currentPerformanceData.setIncreaseBase(50.0);
                            }
                        }
                    }
                    double cumulativeBase = dataEnList.get(i).getCumulativeBase();
                    currentPerformanceData.setCumulativeBase(cumulativeBase);
                    currentPerformanceData.setProportionOfPunishment(maxProp.get(j).getMaxProp());
                    currentPerformanceData.setValueOfPunishment((cumulativeBase + currentPerformanceData.getIncreaseBase())* maxProp.get(j).getMaxProp());
                    double current_base = 0.0;
                    if (isDirect) {
                        current_base = cumulativeBase - currentPerformanceData.getValueOfPunishment() + currentPerformanceData.getIncreaseBase() > 1000.0 ? 1000.0: cumulativeBase - currentPerformanceData.getValueOfPunishment() + currentPerformanceData.getIncreaseBase();
                    }else {
                        current_base = cumulativeBase - currentPerformanceData.getValueOfPunishment() + currentPerformanceData.getIncreaseBase() > 500 ? 500: cumulativeBase - currentPerformanceData.getValueOfPunishment() + currentPerformanceData.getIncreaseBase();
                    }
                    currentPerformanceData.setCurrentBase(current_base);
                    currentPerformanceData.setMoneyEveryMonth(current_base);
                    currentPerformanceData.setMoneyQuarter(current_base * 3);
                    currentPerformanceData.setOnWork(dataEnList.get(i).getEmployee().getIs45Days());

                    // 更新当前季度绩效信息表
                    int flag = currentPerformanceDataMapper.updateByPrimaryKeySelective(currentPerformanceData);
                }
            }
            if (maxProp.size() == 0 || !isMaxProp) { // maxProp.size 为零代表着没有员工有违章信息。
                currentPerformanceData.setEmployeeIdCard(idCard);
                currentPerformanceData.setEvaluateRecordId(idCard);
                if (isDirect) {
                    currentPerformanceData.setIncreaseBase(100.0);
                }else {
                    currentPerformanceData.setIncreaseBase(50.0);
                }
                double cumulativeBase = dataEnList.get(i).getCumulativeBase();
                currentPerformanceData.setCumulativeBase(cumulativeBase);
                currentPerformanceData.setProportionOfPunishment(0.0);
                currentPerformanceData.setValueOfPunishment(0.0);
                currentPerformanceData.setCurrentBase(cumulativeBase + currentPerformanceData.getIncreaseBase());
                currentPerformanceData.setMoneyEveryMonth(cumulativeBase + currentPerformanceData.getIncreaseBase());
                currentPerformanceData.setMoneyQuarter((cumulativeBase + currentPerformanceData.getIncreaseBase()) * 3);
                currentPerformanceData.setOnWork(dataEnList.get(i).getEmployee().getIs45Days());
                currentPerformanceDataMapper.updateByPrimaryKeySelective(currentPerformanceData);
            }
        }
        return false;
    }

    @Override
    public List<String> getEvaluateCardList(Integer year, Integer quarter) {
        return mapper.getEvaluateCardList(year, quarter);
    }
}
